﻿@{
    Layout = "~/Views/Shared/OA/_Splitter.cshtml";
    var roles = ManagerAuthorization.Current.User.Roles;
}
@section Toolbar
{
    @Html.MyNet().Button(new ButtonOptions { Text = "新建", Click = "create" })
    @Html.MyNet().Button(new ButtonOptions { Text = "修改", Click = "modify" })
    @if (!roles.Any(x => x.Key == "Admin_YG"))
    {
        @Html.MyNet().Button(new ButtonOptions { Text = "审核通过", Click = "pass" })
        @Html.MyNet().Button(new ButtonOptions { Text = "审核不通过", Click = "unPass" })
    }
    @Html.MyNet().Button(new ButtonOptions { Text = "移动", Click = "move" })
    @Html.MyNet().Button(new ButtonOptions { Text = "置顶", Click = "setTop" })
    @Html.MyNet().Button(new ButtonOptions { Text = "上移", Click = "moveUp" })
    @Html.MyNet().Button(new ButtonOptions { Text = "下移", Click = "moveDown" })
    @if (Request["parentKey"] == "sy")
    {
        @Html.MyNet().Button(new ButtonOptions { Text = "推荐到栏目", Click = "setCatalogIds" })
    }
    @if (ConvertHelper.ToBoolean(Request["showSignup"]))
    {
        @Html.MyNet().Button(new ButtonOptions { Text = "报名信息", Click = "signup" })
    }
    @Html.MyNet().Button(new ButtonOptions { Text = "删除", Click = "removee" })
}
@section Menu
{
    @*@Html.MyNet().Menu(new ButtonOptions { Text = "查看详细", Click = "detail" })*@
    @Html.MyNet().Menu(new ButtonOptions { Text = "修改", Click = "modify" })
    @Html.MyNet().Menu(new ButtonOptions { Text = "移动", Click = "move" })
    @*@Html.MyNet().Menu(new ButtonOptions { Text = "转链", Click = "transfer" })*@
    @if (!roles.Any(x => x.Key == "Admin_YG"))
    {
        @Html.MyNet().Menu(new ButtonOptions { Text = "审核通过", Click = "pass" })
        @Html.MyNet().Menu(new ButtonOptions { Text = "审核不通过", Click = "unPass" })
    }
    @Html.MyNet().Menu(new ButtonOptions { Text = "删除", Click = "removee" })
}
@section Query
{
    @Html.Partial("_Query", new ArticleCondition())
}
@section Tree
{
    <div id="tree" class="tree">
    </div>
}
@section Main
{
    <div id="grid" class="grid">
    </div>
    <div id="pager" class="pager">
    </div>
    @Html.Hidden("ParentKey", Request["parentKey"])
}
<script type="text/javascript">

    var url = "/CMS/Article/";
    var urlTopic = "/CMS/Topic/";
    var urlSignup = "/CMS/Signup/";

    jQuery(function () {
        queryUnselectTree = false;

        $("#tree").kendoTreeView(
            {
                dataSource:
                {
                    transport: { read: urlTopic + "ReadForTree?ResultType=json" },
                    schema:
                    {
                        data: "Data",
                        model: {text:"Title", children: "childs" },
                        errors: "Errors"
                    },
                    error: function (e) {
                        alert(e.errors);
                    }
                },
                dragAndDrop: true,
                dataBound: onDataBound,
                select: onSelect,
                dragend: onDragend
            });
    });

    function read() {

        $.ajax
            ({
                url: url + "ReadForTopic?VPartial=read",
                data: { topicId: treeSelected()},
                type: "post",
                error: disposeError,
                success: function (result) {

                    if (disposeException(result)) return;
                    bind(result);
                }
            });
    }

    function create() {
        if (treeSelected()) {
            nav(url + "Create?CatalogId=" + treeSelected() + "&parentKey=" + $('#ParentKey').val());
        }
        else {
            nav(url + "Create");
        }
    }

    function modify() {
        if (!gridSelected()) return alert("未选中任何数据！");
        nav(url + "Modify?Id=" + gridSelected());
    }

    function move() {
        if (!gridSelected()) return alert("未选中任何数据！");
        nav(url + "Move?parentKey=" + $('#ParentKey').val() + "&Ids=" + gridSelected().join(','));
    }

    function transfer() {
        if (!gridSelected()) return alert("未选中任何数据！");
        nav(url + "Transfer?Ids=" + gridSelected().join(','));
    }

    function comment() {
        var commentCatalogKey = gridSelectedHidden("CommentCatalogKey");
        if (commentCatalogKey.toString().length == 0) return alert("该栏目未配置所评论栏目！");

        if (!gridSelected()) return alert("未选中任何数据！");
        nav(url + "Comment?Id=" + gridSelected());
    }

    function associate() {
        var associationCatalogKey = gridSelectedHidden("AssociationCatalogKey");
        if (associationCatalogKey.toString().length == 0) return alert("该栏目未配置所关联信息栏目！");

        if (!gridSelected()) return alert("未选中任何数据！");
        nav(url + "Association?AssociationId=" + gridSelected());
    }

    //function detail()
    //{
    //    if (!gridSelected()) return alert("未选中任何数据！");
    //    nav(url + "Detail?Id=" + gridSelected());
    //}

    function removee() {
        if (!gridSelected()) return alert("未选中任何数据！");
        if (!confirm("确定要删除选中的数据吗？")) return;

        $.ajax
            ({
                url: url + "Remove",
                data: { ids: gridSelected() },
                type: "post",

                error: disposeError,
                success: function (result) {
                    if (disposeException(result)) return;

                    read();
                }
            });
    }

    function pass() {
        if (!gridSelected()) return alert("未选中任何数据！");

        $.ajax
            ({
                url: url + "Pass",
                data: { ids: gridSelected() },
                type: "post",
                error: disposeError,
                success: function (result) {
                    if (disposeException(result)) return;

                    read();
                }
            });
    }

    function unPass() {
        if (!gridSelected()) return alert("未选中任何数据！");

        $.ajax
            ({
                url: url + "UnPass",
                data: { ids: gridSelected() },
                type: "post",
                error: disposeError,
                success: function (result) {
                    if (disposeException(result)) return;
                    read();
                }
            });
    }

    function setTop() {
        if (!gridSelected()) return alert("未选中任何数据！");

        $.ajax
            ({
                url: url + "SetTop",
                data: { ids: gridSelected() },
                type: "post",
                error: disposeError,
                success: function (result) {
                    if (disposeException(result)) return;
                    read();
                }
            });
    }

    function moveUp() {
        if (!gridSelected()) return alert("未选中任何数据！");

        $.ajax
            ({
                url: url + "MoveUp",
                data: { id: gridSelected()[0] },
                type: "post",
                error: disposeError,
                success: function (result) {
                    if (disposeException(result)) return;
                    read();
                }
            });
    }

    function moveDown() {
        if (!gridSelected()) return alert("未选中任何数据！");

        $.ajax
            ({
                url: url + "MoveDown",
                data: { id: gridSelected()[0] },
                type: "post",
                error: disposeError,
                success: function (result) {
                    if (disposeException(result)) return;
                    read();
                }
            });
    }

    function signup() {
        if (!gridSelected()) return alert("未选中任何数据！");
        nav(urlSignup + "index?articleId=" + gridSelected());
    }

    function setCatalogIds() {
        if (!gridSelected()) return alert("未选中任何数据！");
        nav(url + "setCatalogIds?id=" + gridSelected());
    }
</script>
